package biznussoft.fieldservice360;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import biznussoft.fieldservice360.dialogfragments.DatePickerDialogFragment;
import biznussoft.fieldservice360.dialogfragments.SignaturetDialogFragmen;
import biznussoft.fieldservice360.dialogfragments.TimePickerDialogFragment;
import biznussoft.fieldservice360.fragments.ActivityDetailFragment;
import biznussoft.fieldservice360.fragments.ExpensesFragment;
import biznussoft.fieldservice360.fragments.ExpensesMasterFragment;
import biznussoft.fieldservice360.fragments.MaterialFragment;
import biznussoft.fieldservice360.fragments.MaterialMasterFragment;
import biznussoft.fieldservice360.fragments.ServiceOrderDetailsFragment;
import biznussoft.fieldservice360.fragments.TimeFragment;
import biznussoft.fieldservice360.fragments.TimeMasterFragment;
import biznussoft.fieldservice360.listeners.ActivityDetailListener;
import biznussoft.fieldservice360.listeners.DialogListener;
import biznussoft.fieldservice360.listeners.GlobalListener;
import biznussoft.fieldservice360.listeners.InventoryListener;
import biznussoft.fieldservice360.utils.Session;


public class ServiceOrderActivity extends BaseActivity  implements DialogListener, InventoryListener,GlobalListener,ActivityDetailListener{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_service_order);
	}


	
	/*@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		  MenuItem actionItem = menu.add("Logout");                 
	        MenuItemCompat.setShowAsAction(actionItem,MenuItemCompat.SHOW_AS_ACTION_ALWAYS | MenuItemCompat.SHOW_AS_ACTION_WITH_TEXT);
	        actionItem.setIcon(R.drawable.logout); 
		return super.onCreateOptionsMenu(menu);
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		 Log.i("NCS","item.getItemId()  :"+item.getTitle()+": "+item.getOrder());
		 if(item.getTitle().equals("Logout")){
			 SalesforceSDKManager.getInstance().logout(this);}
		return super.onOptionsItemSelected(item);
	}*/

	@Override
	public void onWarehouseClicked(final int position) {
		// TODO Auto-generated method stub
		if(position >= 0){                
			ServiceOrderDetailsFragment newFragment = new ServiceOrderDetailsFragment();
			FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
			transaction.replace(R.id.serviceOrderviewer, newFragment);
			transaction.addToBackStack(null);
			transaction.commit();
			new Handler().postDelayed(new Runnable() {                      
				@Override
				public void run() {
					ServiceOrderDetailsFragment warehouseFragment = (ServiceOrderDetailsFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
					warehouseFragment.getWarehouseDetails(position);
				}
			}, 2000);
		}
	}

	@Override
	public void onCategoryClicked(final int position) {
		
		
	}

	@Override
	public void onNewContact(final String[] args) {
		
		/*ActivityDetailFragment newFragment = new ActivityDetailFragment();
		FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
		transaction.replace(R.id.serviceOrderviewer, newFragment);
		transaction.addToBackStack(null);
		transaction.commit();
		new Handler().postDelayed(new Runnable() {                      
			@Override
			public void run() {*/
				ActivityDetailFragment warehouseFragment = (ActivityDetailFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
				warehouseFragment.getSignature(args[0]);
			/*}
		}, 2000);*/
	}

	@Override
	public void onGetContacts(final int position, final String category) {

		
		
		if(position >= 0){                
			ActivityDetailFragment newFragment = new ActivityDetailFragment();
			FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
			transaction.replace(R.id.serviceOrderviewer, newFragment);
			transaction.addToBackStack(null);
			transaction.commit();
			new Handler().postDelayed(new Runnable() {                      
				@Override
				public void run() {
					ActivityDetailFragment warehouseFragment = (ActivityDetailFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
					warehouseFragment.getWarehouseDetails(position,category);
				}
			}, 2000);
		}
	
	
	}

	@Override
	public void getCategory(String category,final String id) {
		if (category.equalsIgnoreCase("time")) {
            
		TimeMasterFragment newFragment = new TimeMasterFragment();
		FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
		transaction.replace(R.id.serviceOrderviewer, newFragment);
		transaction.addToBackStack(null);
		transaction.commit();
		new Handler().postDelayed(new Runnable() {                      
			@Override
			public void run() {
				TimeMasterFragment warehouseFragment = (TimeMasterFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
				warehouseFragment.getTime(id);
			}
		}, 2000);
	
		}
		if (category.equalsIgnoreCase("material")) {
			MaterialMasterFragment newFragment = new MaterialMasterFragment();
			FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
			transaction.replace(R.id.serviceOrderviewer, newFragment);
			transaction.addToBackStack(null);
			transaction.commit();
			new Handler().postDelayed(new Runnable() {                      
				@Override
				public void run() {
					MaterialMasterFragment warehouseFragment = (MaterialMasterFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
					warehouseFragment.getMaterial(id);
				}
			}, 2000);
		}
		if (category.equalsIgnoreCase("expenses")) {
			ExpensesMasterFragment newFragment = new ExpensesMasterFragment();
			FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
			transaction.replace(R.id.serviceOrderviewer, newFragment);
			transaction.addToBackStack(null);
			transaction.commit();
			new Handler().postDelayed(new Runnable() {                      
				@Override
				public void run() {
					ExpensesMasterFragment warehouseFragment = (ExpensesMasterFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
					warehouseFragment.getExpenses(id);
				}
			}, 2000);
		}
	}

	@Override
	public void nextDialog(String type) {
		Log.i("NCS","Type is :"+type);
		if(type.equalsIgnoreCase("date")){
		 DatePickerDialogFragment.newInstace().show(getSupportFragmentManager(), "dialog");}
		if(type.equalsIgnoreCase("time")){
			TimePickerDialogFragment.newInstace().show(getSupportFragmentManager(), "dialog");}
		if(type.equalsIgnoreCase("sign")){
			SignaturetDialogFragmen.newInstace().show(getSupportFragmentManager(), "dialog");
		}
	}

	@Override
	public void getSignature(String base64) {
		// TODO Auto-generated method stub
		ActivityDetailFragment warehouseFragment = (ActivityDetailFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
		warehouseFragment.getSignature(base64);
	}

	@Override
	public void setDateTime(StringBuffer sb) {
		
		Log.i("NCS","Date is  :"+sb.toString());
		
		if(Session.sStartTime && Session.sActivity){
			ActivityDetailFragment warehouseFragment = (ActivityDetailFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
			warehouseFragment.setStartDate(sb.toString());
		}
		if(Session.sEndTime && Session.sActivity){
			ActivityDetailFragment warehouseFragment = (ActivityDetailFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
			warehouseFragment.setEndDate(sb.toString());
		}
		if(Session.sStartTime && Session.sTime){
			TimeFragment timeFragment = (TimeFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
			timeFragment.setStartDate(sb.toString());
		}
		if(Session.sEndTime && Session.sTime){
			TimeFragment timeFragment = (TimeFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
			timeFragment.setEndDate(sb.toString());
		}
		if(Session.sStartTime && Session.sExpense){
			ExpensesFragment expenseFragment = (ExpensesFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
			expenseFragment.setTransactionDate(sb.toString());
		}
		if(Session.sStartTime && Session.sNewTime){
			TimeMasterFragment expenseFragment = (TimeMasterFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
			expenseFragment.setStartDate(sb.toString());
		}
		if(Session.sEndTime && Session.sNewTime){
			TimeMasterFragment expenseFragment = (TimeMasterFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
			expenseFragment.setEndDate(sb.toString());
		}
	}



	@Override
	public void displayFields(final int position, String category) {
		// TODO Auto-generated method stub
		if(category.equalsIgnoreCase("TimeMaster")){
			TimeFragment newFragment = new TimeFragment();
			FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
			transaction.replace(R.id.serviceOrderviewer, newFragment);
			transaction.addToBackStack(null);
			transaction.commit();
			new Handler().postDelayed(new Runnable() {                      
				@Override
				public void run() {
					TimeFragment warehouseFragment = (TimeFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
					warehouseFragment.showRecords(position);
				}
			}, 2000);
		}
		if(category.equalsIgnoreCase("ExpenseMaster")){
			ExpensesFragment newFragment = new ExpensesFragment();
			FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
			transaction.replace(R.id.serviceOrderviewer, newFragment);
			transaction.addToBackStack(null);
			transaction.commit();
			new Handler().postDelayed(new Runnable() {                      
				@Override
				public void run() {
					ExpensesFragment warehouseFragment = (ExpensesFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
					warehouseFragment.showRecords(position);
				}
			}, 2000);
		}
		if(category.equalsIgnoreCase("MaterialMaster")){
			MaterialFragment newFragment = new MaterialFragment();
			FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
			transaction.replace(R.id.serviceOrderviewer, newFragment);
			transaction.addToBackStack(null);
			transaction.commit();
			new Handler().postDelayed(new Runnable() {                      
				@Override
				public void run() {
					MaterialFragment warehouseFragment = (MaterialFragment) getSupportFragmentManager().findFragmentById(R.id.serviceOrderviewer);
					warehouseFragment.showRecords(position);
				}
			}, 2000);
		}
	}

}
